/*
 *  Jun 16, 2009
 */
package app;

import java.io.IOException;
import javax.microedition.io.Connector;
import javax.wireless.messaging.Message;
import javax.wireless.messaging.MessageConnection;
import javax.wireless.messaging.MessageListener;
import javax.wireless.messaging.TextMessage;

/**
 *
 * @author thirdy
 */
public class ThesisClient implements ThesisMessageReceiver {

    private MessageConnection SMSServer;
    private ThesisMIDlet thesisMIDlet;
    private ThesisMessageReader thesisMessageReader;
    private boolean useDefSrvrNum;
    private String defSrvrNum;

    public ThesisClient(final ThesisMIDlet thesisMIDlet) {
        useDefSrvrNum = thesisMIDlet.getClntSettingsFlag()[0];
        defSrvrNum = thesisMIDlet.getDefaultSrvrNum();
        this.thesisMIDlet = thesisMIDlet;
        new Thread(new Runnable() {

            public void run() {
                try {
                    SMSServer = (MessageConnection) Connector.open("sms://:" + ThesisConstants.SMSPORT);
                    thesisMessageReader = new ThesisMessageReader(SMSServer, ThesisClient.this);
                    SMSServer.setMessageListener(new MessageListener() {

                        public void notifyIncomingMessage(MessageConnection conn) {
                            thesisMessageReader.handleMessage();
                        }
                    });
                } catch (Throwable t) {
                    log(t);
                    thesisMIDlet.switchDisplayable(null, thesisMIDlet.getMainMenu());
                    return;
                }
                new Thread(thesisMessageReader).start();
            }
        }).start();
    }

    public void messageReceived(Message mess) {
        log("Reply from server: " + ((TextMessage) mess).getPayloadText());
    }

    public synchronized void sendSMS(String message, String address) {
        final TextMessage textMessage = (TextMessage) SMSServer.newMessage(MessageConnection.TEXT_MESSAGE);
        textMessage.setPayloadText(message);
        textMessage.setAddress("sms://" + address + ":" + ThesisConstants.SMSPORT);
        new Thread() {

            public void run() {
                try {
                    SMSServer.send(textMessage);
                } catch (Throwable t) {
                    log(t);
                    return;
                }
                log("Request Sent");
            }

            ;
        }.start();
    }

    public void destroy() {
        try {
            if (SMSServer != null) {
                SMSServer.close();
            }
            if (thesisMessageReader != null) {
                thesisMessageReader.close();
            }
        } catch (Throwable t) {
            log(t);
        }
        SMSServer = null;
        thesisMessageReader = null;
    }

    private void log(String msg) {
        thesisMIDlet.showInfoAlert(msg);
    }

    private void log(Throwable t) {
        thesisMIDlet.showErrorAlert("Sorry, " + t.getMessage());
    }
}
